<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<!-- 引入easyui 的主题样式  和  easyui 提供的图标样式 -->
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/themes/bootstrap/easyui.css" />
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/themes/icon.css" />
<!-- 引入jquery 和  easyui.js   中文设置js-->
<script type="text/javascript"
	src="${pageContext.request.contextPath}\js\jquery-1.9.1.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/easyui-lang-zh_CN.js"></script>
</head>
<script type="text/javascript">
$(function(){
	$('#tb').datagrid({
		url:'showCar',
		rownumbers:true,
		fitColumns:true,
		pagination:true,
		pageSize:5,
		pageList:[5,10,15],
		loadMsg:'正在加载，请稍后...',
		columns:[[ 
		           {field:'ck',checkbox:true,width:100,align:'center'},
		           {field:'carId',title:'编号',width:100,align:'center',hidden:true},
		           {field:'carName',title:'汽车品牌',width:100,align:'center'},    
		           {field:'carType',title:'车型',width:100,align:'center'},    
		           {field:'carMaxWeight',title:'最大载重(吨)',width:100,align:'center'},
		           {field:'carNumber',title:'车牌号',width:100,align:'center'},
		           {field:'carState',title:'车辆状态',width:100,align:'center'},
		           {field:'dpName',title:'配送点',width:100,align:'center'},
		       ]],
		toolbar: ['-',{
					text:'购买车辆',
			   		handler: function(){
			   		$('#win').window('open');	
			   		}
			   	},'-',{
			   		text:'车辆调配',
			   		handler: function(){
			   			var selects = $('#tb').datagrid('getSelections');
						if(selects.length==1){
							var ck = $('#tb').datagrid('getChecked');
							var carName = ck[0].carName;
							var carType = ck[0].carType;
							var carNumber = ck[0].carNumber;
							var dpName = ck[0].dpName;
								$.messager.confirm('提示信息', '您正在调配“' + carName +'牌'+carType+ '”是否继续？',
										function(r) {
							if (r) {
							$('#win1').window('open');
							$('#cc').combobox('setValue',dpName);
							}
						});
						} else if (selects.length == 0) {
							$.messager.show({
								width:300,
								height:120,
								timeout:3000,
								title:'提示',
								msg:'请选择您要调配的车辆！',
								showType:'fade',
								style:{
									top:document.body.scrollTop+document.documentElement.scrollTop,
								}
							});
						} else {
							$.messager.confirm('提示','请选择单辆车进行调配', function(r) {
										if (r) {
												$('#tb').datagrid('uncheckAll');
										}
									});
						}

			   		}
			   	},'-']

	});
	//购买窗口
	$('#win').window({
		title : "购买车辆",
		width : 500,
		height : 350,
		modal:true,
		collapsible : false,
		minimizable : false,
		maximizable : false,
		shadow:false,
		resizable:false,
		draggable:false
	});
	//按钮"确认"
		$('#bt1').linkbutton({
			width:55,
			height:32,
			onClick:function(){
				var carName = $('#c1').combo('getText');
				var carType = $('#c2').combo('getText');
				var carNumber = $('#c3').combo('getText');
				var dpName = $('#cb').combo('getText');
				if(carName.length==0||carType.length==0||carNumber.length==0||dpName.length==0){
					alert(carName.length+","+carType.length+","+carNumber.length+","+dpName.length);
					$.messager.alert('提示','请完善您想购买的车辆信息！')
				}else{
					$.ajax({
						url:'buycar',
						data:{'carName':carName,'carType':carType,'carNumber':carNumber,'dpName':dpName},
						dataType:'json',
						type:'post',
						success:function(buy){
							if(buy==0){
								$.messager.show({
									width:300,
									height:120,
									timeout:3000,
									title:'购买成功！',
									msg:'您购买的“'+carName+'重型卡车”，最大载重10吨，售价410,000元，已购买成功！',
									showType:'fade',
									style:{
										top:document.body.scrollTop+document.documentElement.scrollTop,
									}
								});
								$('#c1').combo('clear');
								$('#c2').combo('clear');
								$('#c3').combo('clear');
								$('#cb').combo('clear');
								$('#win').window('close');
								$('#tb').datagrid('reload');
							}else if(buy==1){
								$.messager.show({
									width:300,
									height:120,
									timeout:3000,
									title:'购买成功！',
									msg:'您购买的“'+carName+'中型卡车”，最大载重6吨，售价360,000元，已购买成功！',
									showType:'fade',
									style:{
										top:document.body.scrollTop+document.documentElement.scrollTop,
									}
								});
								$('#c1').combo('clear');
								$('#c2').combo('clear');
								$('#c3').combo('clear');
								$('#cb').combo('clear');
								$('#win').window('close');
								$('#tb').datagrid('reload');		
							}else if(buy==2){
								$.messager.show({
									width:300,
									height:120,
									timeout:3000,
									title:'购买成功！',
									msg:'您购买的“'+carName+'小型卡车”，最大载重2吨，售价50,000元，已购买成功！',
									showType:'fade',
									style:{
										top:document.body.scrollTop+document.documentElement.scrollTop,
									}
								});
								$('#c1').combo('clear');
								$('#c2').combo('clear');
								$('#c3').combo('clear');
								$('#cb').combo('clear');
								$('#win').window('close');
								$('#tb').datagrid('reload');
							}else if(buy==3){
								$.messager.alert('选购失败','车牌已被选用，请给新车配发其他车牌后再购买！');
								$('#c3').combo('clear');
							}
							
						}
					})
				}
				
			}
			
		});
		//按钮"取消"
		$('#bt2').linkbutton({
			width:55,
			height:32,
			onClick:function(){
				$('#c1').combo('clear');
				$('#c2').combo('clear');
				$('#c3').combo('clear');
				$('#cb').combo('clear');
				$('#win').window('close');
			}
			
		});
		//车辆调配窗口
		$('#win1').window({
			title : "车辆调配",
			width : 500,
			height : 350,
			modal:true,
			collapsible : false,
			minimizable : false,
			maximizable : false,
			shadow:false,
			resizable:false,
			draggable:false
		});
		//配送点下拉框
		$('#cc').combobox({    
		    url:'combo',    
		    valueField:'id',    
		    textField:'text',
		});
		//配送点下拉框
		$('#cb').combobox({    
		    url:'combo',    
		    valueField:'id',    
		    textField:'text',
		});
		//车辆调配弹窗取消按钮
		$('#bt').linkbutton({
			text:"取消",
			width:55,
			height:32,
			onClick:function(){
				$('#win1').window('close');
			}
		});
		//车辆调配弹窗确认按钮
		$('#btn').linkbutton({
			text:"提交",
			width:55,
			height:32,
		    onClick:function(){
		    	var ck = $('#tb').datagrid('getChecked');
		    	var carId = ck[0].carId;//车辆id
		    	var olddpName = ck[0].dpName;//前配送点
		    	var newdpName = $('#cc').combobox('getText');//新配送点
		    	if(newdpName.length==0){
		    		$.messager.alert('提示','请完善调配信息！')
		    	}else{
						$.ajax({
			    			url:'assignCar2',
			    			data:{'carId':carId,'newdpName':newdpName},
			    			dataType:'json',
			    			type:'post',
			    			success:function(assign){
			    				if(assign==0){
			    					$.messager.show({
										width:300,
										height:120,
										title:'提示',
										msg:'成功调配车辆！',
										showType:'fade',
										timeout:3000,
										style:{
											top:document.body.scrollTop+document.documentElement.scrollTop,
										}
									});
			    					$('#cb').combobox('clear');
			    					$('#win1').window('close');
									$('#tb').datagrid('reload');
			    				}else{
			    					$.messager.alert('提示','车辆调配失败，请重试！');
			    					$('#cb').combobox('clear');
			    				}
			    			}
						})
				}
			}
							})
						

	})
</script>

<body>
<!-- 车辆信息展示 -->
	<table id="tb"></table>
<!-- 购买车辆窗口 -->	
	<div id = "win" closed="true">
	<div style="margin-top:50px;margin-left:100px">
		<span>汽车品牌 ：</span><select id="c1" class="easyui-combobox" name="carbrand"
		data-options="editable:false" style="width:200px;">   
    		<option></option>
    		<option>华凌</option>   
    		<option>红岩</option>   
    		<option>东风</option>   
    		<option>庆铃</option>   
    		<option>福田</option>
    		<option>奇瑞</option>   
		</select>
		<br><br> 
		<span>车型选择 ：</span><select id="c2" class="easyui-combobox" name="cartype" 
		data-options="editable:false" style="width:200px;">   
    		<option></option>
    		<option>重型卡车</option>   
    		<option>中型卡车</option>   
    		<option>小型卡车</option>   
		</select>
		<br><br>
		<!-- 配发车牌下拉框 -->
			</select> 
			<span>配发车牌 ：</span><select id="c3" class="easyui-combobox" name="carnum" 
				editable="false" style="width:200px;">
				<option></option>
    			<option>渝D44577</option>   
    			<option>渝D77566</option>   
    			<option>渝D66561</option>   
    			<option>渝D55562</option>   
    			<option>渝D44562</option>
    			<option>渝D33562</option>   
			</select>
			<br><br>
			<!-- 配送点选择的下拉框 -->
      		<span>配送点选择：</span><input id="cb" name="dpName" 
      				style="width:200px;" editable="false">
			<br><br>
		<div style="margin-left:90px">
		<input id="bt1" type="button" value="确认"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<input id="bt2" type="button" value="取消"/>
		</div> 
		
	</div>
	</div>
<!-- 车辆调配窗口 -->
	<div id ="win1" closed="true">
		<div style="margin-top:50px;margin-left:100px">
        	<!-- 配送点选择的下拉框 -->
      		<span>配送点选择：</span><input id="cc" name="dpName" 
      				style="width:200px;" editable="false">
			<br><br><br>
			<div style="margin-left:90px">
				<input id="btn" type="button" value="提交"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				<input id="bt" type="button" value="取消"/>
			</div>
		</div>
	</div>
</body>
</html>